package com.example.petplatform.service;

import com.example.petplatform.dao.AdoptionApplicationDao;
import com.example.petplatform.dao.PetDao;
import com.example.petplatform.entity.AdoptionApplication;
import com.example.petplatform.entity.Param.Params;
import com.example.petplatform.entity.Pet;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdoptionApplicationService {

    @Resource
    private AdoptionApplicationDao adoptionApplicationDao;
    @Resource PetDao petDao;
    public List<AdoptionApplication> getAllApplications() {
        return adoptionApplicationDao.getAllApplications();
    }

    public PageInfo<AdoptionApplication> applicationSearch(Params params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<AdoptionApplication> list = adoptionApplicationDao.applicationSearch(params);
        return PageInfo.of(list);
    }

    public List<AdoptionApplication> applicationAdd(AdoptionApplication application) {
        adoptionApplicationDao.insertSelective(application);
        return adoptionApplicationDao.getAllApplications();
    }
    public List<AdoptionApplication> applicationUpdate(AdoptionApplication application) {
        adoptionApplicationDao.updateByPrimaryKeySelective(application);
        System.out.println(application.getPet_id());
        //如果审核状态为：approved 则修改宠物领养的状态为adopted
        if ("approved".equals(application.getStatus())) {
            Pet pet = new Pet();
            pet.setId(application.getPet_id());
            pet.setStatus("adopted");
            petDao.updateByPrimaryKeySelective(pet);
        }
        return adoptionApplicationDao.getAllApplications();
    }

    public List<AdoptionApplication> applicationDelete(Integer id) {
        adoptionApplicationDao.deleteByPrimaryKey(id);
        return adoptionApplicationDao.getAllApplications();
    }
}
